/*
 * To change this template, choose Tools | Templates and open the template in
 * the editor.
 */
package com.oop.example.work.bel;

import com.oop.example.xml.Block;
import com.oop.example.xml.Entry;
import com.oop.example.xml.ObjectFactory;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Properties;
import javax.xml.bind.JAXBElement;

/**
 *
 * @author -
 */
public class Utils {

    /**
     * Ударение ́
     */
    public static String ACCENT_SYMBOL = "́";
    public static final String LabelDictionaryFile = "labels.xml";
    public static final String LangsFile = "lang.xml";
    public static final String LabelDictionaryFileEncoding = "UTF-8";
    public static final String LangsFileEncoding = "UTF-8";
    public static final String NL = System.getProperty("line.separator");
    public static String[] homonymNumeration = {"X", "IX", "VIII", "VII", "VI", "IV", "V", "III", "II", "I"};
    public static final List<String> noLabelLabels = new ArrayList<String>();//{"", "",};

    static {
        noLabelLabels.add("-л.");
        noLabelLabels.add("и т. д.");
        noLabelLabels.add("и т. п.");
        noLabelLabels.add("т. д.");
        noLabelLabels.add("т. п.");
        noLabelLabels.add("и пр.");
    }

    public static Properties getLabelDictionary() {
        Properties props = new Properties();
        try {
            props.loadFromXML(new FileInputStream(LabelDictionaryFile));
        } catch (Exception ex) {
            ex.printStackTrace(System.out);
        }
        return props;
    }

    public static Properties getLangs() {
        Properties props = new Properties();
        try {
            props.loadFromXML(new FileInputStream(LangsFile));
        } catch (Exception ex) {
            ex.printStackTrace(System.out);
        }
        return props;
    }

    public static void main(String[] args) {
        ObjectFactory factory = new ObjectFactory();
        Entry.Heading entryHeading = factory.createEntryHeading();
        boolean isPrimary = true;
        String hdr = "";
        hdr = hdr.trim();
        //Удаляем римские цифры из хвоста
        for (String rom : Utils.homonymNumeration) {
            hdr = hdr.replace(rom, "");
        }
        Entry.Heading.Vocable entryHeadingVocable = factory.createEntryHeadingVocable();

        Entry.Heading.Vocable.Part entryHeadingVocablePart = factory.createEntryHeadingVocablePart();
        Block block = new Block();
        block.setValue(hdr);
        JAXBElement<Block> simple = factory.createEntryHeadingVocablePartSimple(block);
        entryHeadingVocablePart.getPartChildren().add(simple);
        entryHeadingVocable.getPart().add(entryHeadingVocablePart);
        entryHeading.getVocable().add(entryHeadingVocable);
        isPrimary = false;

        //entry.setHeading(entryHeading);

    }
    private static final char[] hexChar = {
        '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
    };

    private static String unicodeEscape(String s) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if ((c >> 7) > 0) {
                sb.append("\\u");
                sb.append(hexChar[(c >> 12) & 0xF]); // append the hex character for the left-most 4-bits
                sb.append(hexChar[(c >> 8) & 0xF]);  // hex for the second group of 4-bits from the left
                sb.append(hexChar[(c >> 4) & 0xF]);  // hex for the third group
                sb.append(hexChar[c & 0xF]);         // hex for the last group, e.g., the right most 4-bits
            } else {
                sb.append(c);
            }
        }
        return sb.toString();
    }

    private static String toUnicodeEscapeString(String str) {
        // Modeled after the code in java.util.Properties.save()
        StringBuffer buf = new StringBuffer();
        int len = str.length();
        char ch;
        for (int i = 0; i < len; i++) {
            ch = str.charAt(i);
            switch (ch) {
                case '\\':
                    buf.append("\\\\");
                    break;
                case '\t':
                    buf.append("\\t");
                    break;
                case '\n':
                    buf.append("\\n");
                    break;
                case '\r':
                    buf.append("\\r");
                    break;

                default:
                    if (ch >= ' ' && ch <= 127) {
                        buf.append(ch);
                    } else {
                        buf.append('\\');
                        buf.append('u');
                        buf.append(toHex((ch >> 12) & 0xF));
                        buf.append(toHex((ch >> 8) & 0xF));
                        buf.append(toHex((ch >> 4) & 0xF));
                        buf.append(toHex((ch >> 0) & 0xF));
                    }
            }
        }
        return buf.toString();
    }

    private static char toHex(int nibble) {
        return hexChar[(nibble & 0xF)];
    }
}
